// pages/scoreMall/scoreOrderDetails/scoreOrderDetails.js
const orderService = require('../../../api/orderService.js');
const wxbarcode = require('../../../utils/self.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: '1014004043803873281',
    order: {},
    goodsList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.orderId) {
      this.getOrderDetails(options.orderId)
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取订单详情
   */
  getOrderDetails: function (orderId) {
    const self = this
    orderService.getDetailsById(orderId).then(res => {
      console.log(res)
      let tempData = {
        ...res,
        status: self.handleOrderStatus(res.status),
        cashTotal: (res.cashTotal).toFixed(2)
      }
      let scoreTotal = 0
      for (let i = 0; i < res.products.length; i++) {
        const ele = res.products[i];
        scoreTotal += ele.exchangeScores
      }
      this.setData({
        order: tempData,
        code: res.ladingCode,
        goodsList: res.products,
        scoreTotal: scoreTotal
      })
      wxbarcode.barcode('barcode', res.ladingCode, 669, 122);
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 处理自提码
   */
  codeTextPartition: function (val) {
    console.log(val)
    val = val.replace(/\s/g, '').replace(/(\w{4})(?=\w)/g, "$1 ");
    console.log(val)
    return val;
  },
  /**
   * 操作订单状态
   */
  handleOrderStatus: function (status) {
    if (status === "CREATED") {
      return "待付款"
    } else if (status === "PAID") {
      return "已付款"
    } else if (status === "SHIPPED") {
      return "配送中"
    } else if (status === "RECEIVED") {
      return "待评价"
    } else if (status === "FINISHED") {
      return "已完成"
    } else if (status === "CANCELED") {
      return "已作废"
    } else if (status === "REJECTED") {
      return "已退款"
    } else if (status === "RETURNING") {
      return "退款中 "
    } else if (status === "PENDING") {
      return "待自提"
    } else if (status === "WAITSHIPPED") {
      return "待发货"
    } else if (status === "WAITPAYBALANCE") {
      return "待付尾款"
    } else {
      return "状态异常"
    }
  },

  /**
   * 联系门店
   */
  contactStore: function () {
    const that = this
    const storeId = this.data.order.storeId
    let storeInfo = {
      tel: ''
    }
    try {
      let allStores = wx.getStorageSync('_allStores_')
      if (allStores && allStores.length > 0) {
        for (let i = 0; i < allStores.length; i++) {
          const item = allStores[i]
          if (item.id == storeId) {
            storeInfo = item;
            break;
          }
        }
      }
    } catch (e) { }
    if (storeInfo.tel !== '') {
      wx.showModal({
        title: '提示',
        content: '确定要拨打以下电话' + storeInfo.tel + '?',
        confirmText: '拨打',
        success: function (res) {
          if (res.confirm) {
            wx.makePhoneCall({
              phoneNumber: storeInfo.tel,
            })
          }
          if (res.cancel) {

          }

        }
      })
    }
  },

  /**
   * 再来一份
   */
  payAgain: function () {
    const tempProducts = this.data.goodsList
    try {
      let scoreMall = {
        allPrice: this.data.order.orderAmount,
        storeId: this.data.order.storeId,
        goodsList: tempProducts,
        orderType: 'scoreMall',
        score: this.data.scoreTotal
      }
      console.log(scoreMall)
      wx.setStorageSync('wj_shopcart', scoreMall)
    } catch (e) {
      console.log(e)
    }
    // 判断是否绑定手机号
    wx.navigateTo({
      url: '../perfectOrder/perfectOrder'
    })
  }
})